package com.group6.exam.manager.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.entity.UserInfoEntity;
import com.group6.exam.manager.dao.IManagerDao;
import com.group6.exam.manager.dao.IUserRoleDao;
import com.group6.exam.manager.dao.ManagerDaoImpl;
import com.group6.exam.manager.dao.UserRoleDaoImpl;

public class ManagerServiceImpl implements IManagerService {

	private IManagerDao managerDao = new ManagerDaoImpl();
	private IUserRoleDao roleDao = new UserRoleDaoImpl();

	@Override
	public boolean login(UserInfoEntity user) {
		// TODO Auto-generated method stub
		return managerDao.login(user);
	}

	@Override
	public List<Map<String, Object>> getMainMenus(String username) {
		// TODO Auto-generated method stub
		return managerDao.getMainMenus(username);
	}

	@Override
	public List<Map<String, Object>> getChildMenus(String username) {
		// TODO Auto-generated method stub
		return managerDao.getChildMenus(username);
	}

	@Override
	public int addUser(UserInfoEntity user) {
		return managerDao.addUser(user);
	}

	@Override
	public List<Map<String, Object>> getUserInfo() {
		List<Map<String, Object>> list = managerDao.getUserInfo();
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> map : list) {
			// 获取权限编号
			String roleId = map.get("role_id") + "";
			// 通过roleId获取roleName
			String roleName = roleDao.getTRoleNameById(roleId);
			map.put("role_name", roleName);

			mapList.add(map);
		}
		System.out.println(mapList);
		return mapList;

	}

	@Override
	public int deleteUserbyId(String userId) {
		return managerDao.deleteUserbyId(userId);
	}

	@Override
	public Map<String, Object> getUserInfoById(String userId) {
		// TODO Auto-generated method stub
		return managerDao.getUserInfoById(userId);
	}

	@Override
	public int updateUserbyId(UserInfoEntity user) {
		return managerDao.updateUserbyId(user);
	}

	@Override
	public PageModel check(PageModel pageModel) {
		// 查询总条数
		int totalRecords = managerDao.getTotalRecords();
		int pageSize = pageModel.getPageSize();
		// 获取总页数
		PageModel page = new PageModel(totalRecords, pageSize);
		// 设置当前页
		page.setCurrentPage(pageModel.getCurrentPage());
		List<Map<String, Object>> mapList = new ArrayList<>();
		List<Map<String, Object>> List = managerDao.check(pageModel);
		for (Map<String, Object> map : List) {
			String role_id = map.get("role_id") + "";
			String rolename = managerDao.getRoleName(role_id);
			map.put("role_name", rolename);
			mapList.add(map);
		}
		page.setList(mapList);
		return page;
	}

}
